package com.bocai.service.impl;

import com.bocai.bean.SysUserInfo;
import com.bocai.bean.TUserCode;
import com.bocai.bean.TUserInfo;
import com.bocai.mapper.CashInfoMapper;
import com.bocai.mapper.SysUserInfoMapper;
import com.bocai.mapper.TUserCodeMapper;
import com.bocai.service.IUserService;
import com.bocai.mapper.TUserInfoMapper;
import com.bocai.utils.CodeEnum;
import com.bocai.utils.MD5Utils;
import com.bocai.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * Created by tu_pei on 2018/4/9.
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private TUserInfoMapper userInfoMapper;

    @Autowired
    private SysUserInfoMapper sysUserInfoMapper;

    @Autowired
    private CashInfoMapper cashInfoMapper;

    @Autowired
    private TUserCodeMapper userCodeMapper;

    @Override
    public int deleteByPrimaryKey(Integer userId) {
        return userInfoMapper.deleteByPrimaryKey(userId);
    }

    @Override
    public int insertSelective(TUserInfo record) {
        return userInfoMapper.insertSelective(record);
    }

    @Override
    public TUserInfo selectByPrimaryKey(Integer userId) {
        return userInfoMapper.selectByPrimaryKey(userId);
    }

    @Override
    public TUserInfo selectUserInfo(String phone) {
        TUserInfo userInfo = userInfoMapper.selectByPrimaryKeyPhone(phone);
        if (null == userInfo || null == userInfo.getPassword() || userInfo.getPassword().trim().length() <= 0) {
            return null;
        } else {
            return userInfo;
        }
    }

    @Override
    public int updateByPrimaryKeySelective(TUserInfo record) {
        return userInfoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(TUserInfo userInfo) {
        return userInfoMapper.updateByPrimaryKey(userInfo);
    }

    @Override
    public int updatePasswordByPhone(TUserInfo userInfo) {
        return userInfoMapper.updateUserPwdByPhone(userInfo);
    }

    @Override
    public SysUserInfo selectSysUserInfo(String accountNumber) {
        return sysUserInfoMapper.selectByPrimaryKeyPhone(accountNumber);
    }

    @Override
    public ResponseData selectUserInfoAll(Integer accountNumberId, String phone, ResponseData data) {
        if (null == accountNumberId || accountNumberId <= 0) {
            data.setCodeEnum(CodeEnum.CODE_90002);
        } else {
            SysUserInfo sysUserInfo = sysUserInfoMapper.selectByPrimaryKey(accountNumberId);
            if (null == sysUserInfo) {
                data.setCodeEnum(CodeEnum.CODE_90003);
            } else {
                data.setCodeEnum(CodeEnum.SUCCESS);
                data.setResults(userInfoMapper.selectAllUserInfo(phone));
            }
        }
        return data;
    }

    @Override
    public ResponseData updateUserInfo(Integer userId, Integer userStatus, Integer accountNumberId, ResponseData data) {
        if (null == accountNumberId || accountNumberId <= 0) {
            data.setCodeEnum(CodeEnum.CODE_90002);
        } else {
            SysUserInfo sysUserInfo = sysUserInfoMapper.selectByPrimaryKey(accountNumberId);
            if (null == sysUserInfo) {
                data.setCodeEnum(CodeEnum.CODE_90003);
            } else {
                data.setCodeEnum(CodeEnum.SUCCESS);
                HashMap<String, Object> map = new HashMap<>();
                map.put("userStatus", userStatus);
                map.put("userId", userId);
                userInfoMapper.updateUserState(map);
            }
        }
        return data;
    }

    @Override
    public ResponseData updatePassword(Integer userId, ResponseData data) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("password", MD5Utils.md5Password("123456"));
        if (userInfoMapper.updateUserPwd(map) == 1) {
            data.setCodeEnum(CodeEnum.SUCCESS);
        }
        return data;
    }

    @Override
    public TUserCode insertCode(TUserCode tUserCode) {
        TUserCode tUserCode1 = userCodeMapper.selectByPrimaryKey(tUserCode.getUserPhone());

        int code = 0;

        if (null != tUserCode1){
            code = userCodeMapper.updateByPrimaryKeySelective(tUserCode);
        }else {
            code = userCodeMapper.insertSelective(tUserCode);
        }

        if (code > 0) {
            return tUserCode;
        } else
            return null;
    }

    @Override
    public TUserCode selectUserCode(String userPhone) {
        return userCodeMapper.selectByPrimaryKey(userPhone);
    }

}
